//
//  WCOtherViewController.m
//  WeChat
//
//  Created by lin on 15/7/14.
//  Copyright (c) 2015年 lin. All rights reserved.
//

#import "WCOtherViewController.h"
#import "CategoryWF.h"
#import "AppDelegate.h"


@interface WCOtherViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightConstraint;
@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation WCOtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"其他方式登录";
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        self.leftConstraint.constant = 10;
        self.rightConstraint.constant = 10;
    }
    self.userField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    self.pwdField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    [self.loginBtn setResizeN_BG:@"fts_green_btn" H_BG:@"fts_green_btn_HL"];
}

- (IBAction)loginBtnClick:(UIButton *)sender {
    //1.把用户名和密码放到沙盒里

    WCUserInfo *userInfo = [WCUserInfo sharedWCUserInfo];
    userInfo.usr = self.userField.text;
    userInfo.pwd = self.pwdField.text;
    
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setObject:usr forKey:@"usr"];
//    [defaults setObject:pwd forKey:@"pwd"];
//    [defaults synchronize];
    [super login];
//    [self.view endEditing:YES];
//    //2.调用appdelegte登录方法
//    [MBProgressHUD showMessage:@"正在登录中" toView:self.view];
//    AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
//    
//    __weak typeof(self) selfVC = self;
//    [appDelegate xmppUserLogin:^(XMPPResultType type) {
//    [selfVC handleResultType:type];
//    }];
    
    
}

-(void)handleResultType:(XMPPResultType)type{
    dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view];
    switch (type) {
        case XMPPResultTypeLoginSuccess:
            [self enterMainPage];
            break;
            
        case XMPPResultTypeLoginFailure:
            [MBProgressHUD showError:@"用户名或密码不正确"];
            break;
            
        case XMPPResultTypeNetErr:
            [MBProgressHUD showError:@"网络问题"];
            break;
    } });

}
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)enterMainPage{
    //更改用户的登录状态为yes
    [WCUserInfo sharedWCUserInfo].loginStatus = YES;
    //把用户登录成功的数据保存到沙盒
    [[WCUserInfo sharedWCUserInfo] saveUserInfoToSandbox];
    
    // 隐藏model窗口, 不然释放不掉
    [self dismissViewControllerAnimated:YES completion:nil];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.view.window.rootViewController = storyBoard.instantiateInitialViewController;
}

-(void)dealloc{
      WCLog(@"%s",__func__);
}



@end
